package BaseObject;

public class Itinerary1 extends Itinerary{

        public Itinerary1(Client c1, Client c2) {
                super(c1,c2);
        }

	public boolean computeItinerary(double base)
	{
		double probaC1 = Tools.N(15.0,5.0, Motor.currentTime - this.clientItinerary.get(0).getAppearingTime() + base * Motor.stepTime );
		double probaC2 = Tools.N(15.0,5.0, Motor.currentTime - this.clientItinerary.get(1).getAppearingTime() + ( base + this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(1).getPosition()) ) * Motor.stepTime );
		probaC1 = 1 - probaC1;
		probaC2 = 1 - probaC2;
		double probaC1ANDC2 = probaC1 *probaC2 ;
		double probaC2C1 = probaC1ANDC2 / probaC1;
		this.probability = probaC2C1;
		
		//Compute the minimum length for client's rides
		double minLengthC1 = this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(0).getDestination());
		double minLengthC2 = this.clientItinerary.get(1).getPosition().distance(this.clientItinerary.get(1).getDestination());
		
		
		//C1's way
		this.length = this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(1).getPosition());
		this.length += this.clientItinerary.get(1).getPosition().distance(this.clientItinerary.get(0).getDestination());
		
		
		//Verify the threeshold condition
		
		double d1=minLengthC1 * (1 + this.clientItinerary.get(0).getMaxDeviationThreeshold());
		if(d1 < this.length)
		return false;
		
		//C2's way
		double lengthC2 = this.clientItinerary.get(1).getPosition().distance(this.clientItinerary.get(0).getDestination());
		lengthC2 += this.clientItinerary.get(0).getDestination().distance(this.clientItinerary.get(1).getDestination());
		
			
		//Verify the threeshold condition
		
		if(minLengthC2 * (1 + this.clientItinerary.get(1).getMaxDeviationThreeshold()) < lengthC2)
			return false;
		
		this.length += this.clientItinerary.get(0).getDestination().distance(this.clientItinerary.get(1).getDestination());
		this.length = this.length - base;
		
		
		return true;
	}

	
	public void moveThisTaxi(Taxi t)
	{
		boolean change=false;
		if(t.getTekenClientList().size() == 2 )
		{
		
			change = t.deplacerTaxi(t.getTekenClientList().get(0).getDestination());
			//si l'on depose le client 1
			if(change)
			{
		
				Client c= t.getTekenClientList().get(0);
				t.getTekenClientList().remove(0);
				Motor.clientsTaken.remove(c); 
			}
		}
		else if(t.getTekenClientList().size()==1 && t.getItinerary() != null)
		{
			//S'il a le premier client
			if(t.getTekenClientList().get(0).equals(t.getItinerary().clientItinerary.get(0)))
			{
				 change = t.deplacerTaxi(t.getItinerary().clientItinerary.get(1).getPosition());
				//si l'on arrive sur la position du second client
				if(change)
				{
					//si celui ci est present
					Client c1= t.getItinerary().clientItinerary.get(1);
					if(Motor.clients.contains(c1))
					{
						t.getTekenClientList().add(c1);
						Motor.clientsTaken.add(c1);
						Motor.clients.remove(c1);
						Motor.increaseNbClientDone();
					}
					else	// s'il celui ci a disparu depuis
					{
						Itinerary4 it = new Itinerary4(t.getItinerary().clientItinerary.get(0), null);
						t.setItinerary(it);
						
					}
					
				}
			}
			
			else	// S'il depose le deuxieme client
			{			
				System.out.println("Should not happen");
				Itinerary4 it = new Itinerary4(t.getItinerary().clientItinerary.get(0), null);
				t.setItinerary(it);
			}
		}
	}

}
